package com.magic.smartcity.view.more;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.magic.smartcity.R;
import com.magic.smartcity.beans.ServiceListBean;
import com.magic.smartcity.databinding.FragmentMoreBinding;
import com.magic.smartcity.interfaces.ISystemServicesCallBack;
import com.magic.smartcity.interfaces.ISystemServicesPresenter;
import com.magic.smartcity.utils.Constant;
import com.magic.smartcity.view.adapters.HomeServiceAdapter;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class MoreFragment extends Fragment implements ISystemServicesCallBack {

    @Inject
    ISystemServicesPresenter mPresenter;
    private FragmentMoreBinding mBinding;
    private HomeServiceAdapter mServiceAdapter;
    private List<ServiceListBean.RowsBean> list = new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mBinding = FragmentMoreBinding.inflate(getLayoutInflater());
        mPresenter.getAllService();//获取系统中所有的应用服务
        mPresenter.registerCallBack(this);

        return mBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.unRegisterCallBack(this);
    }

    @Override
    public void AllServiceLoaded(List<ServiceListBean.RowsBean> mData) {
        Log.d("MoreFragment", "成功获取了全部服务应用的数据:::::" + mData.size());
        mData.sort(new MySort());
        list.clear();
        list.addAll(mData);
        initServices();

    }

    private void initServices() {//先进行按类型进行排序;
        String indexString = null;
        //创建根布局
        LinearLayout rootline = mBinding.rootView;
        GridLayout gridLayout = null;
        LinearLayout line;
        for (int i = 0; i < list.size(); i++) {
            ServiceListBean.RowsBean service = list.get(i);
            View icon_view;
            ImageView icon_image;
            TextView icon_text;
            LinearLayout.LayoutParams gridParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);


            //初始化第一个应用类型区域
            if (i == 0) {
                indexString = service.getServiceType();//将坐标初始化，第一个服务应用类型

                //首先创建一线条和类型名字
                line = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_more_line, null);
                TextView type_text = line.findViewById(R.id.service_type);
                type_text.setText(service.getServiceType());//设置类型名字
                rootline.addView(line);//在根布局上添加一行类型分割条//将布局添加到根布局上!!!!!!

                //再创建应用服务图标区域
                gridLayout = new GridLayout(getContext());
                gridLayout.setLayoutParams(gridParams);
                gridLayout.setColumnCount(5);//一排5个
                //获取应用服务图标与名字布局
                icon_view = LayoutInflater.from(getContext()).inflate(R.layout.item_service_topic, (ViewGroup) getView(), false);
                icon_image = icon_view.findViewById(R.id.service_icon);
                icon_text = icon_view.findViewById(R.id.service_title);
                //从网络加载数据填充应用服务图标
                Glide.with(icon_view).load(Constant.Service_IP + service.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon_image);
                icon_text.setText(service.getServiceName());//填充应用服务的名称
                gridLayout.addView(icon_view);//将应用图标添加进图标区域中
                //第一个类型第一个服务应用初始化完毕......
            } else if (i == list.size() - 1) {//将最后一个服务应用放进视图
                if (!indexString.equals(service.getServiceType())) {//如果最后一个是新类型，创建新的服务区域
                    rootline.addView(gridLayout);//将上一个服务区域放进根布局
                    indexString = service.getServiceType();//将坐标初始化，新的服务应用类型
                    //首先创建一线条和类型名字
                    line = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_more_line, null);
                    TextView type_text = line.findViewById(R.id.service_type);
                    type_text.setText(service.getServiceType());//设置类型名字
                    rootline.addView(line);//在根布局上添加一行类型分割条//将布局添加到根布局上!!!!!!
                    //再创建应用服务图标区域
                    gridLayout = new GridLayout(getContext());
                    gridLayout.setLayoutParams(gridParams);
                    gridLayout.setColumnCount(5);//一排5个
                    //获取应用服务图标与名字布局
                    icon_view = LayoutInflater.from(getContext()).inflate(R.layout.item_service_topic, (ViewGroup) getView(), false);
                    icon_image = icon_view.findViewById(R.id.service_icon);
                    icon_text = icon_view.findViewById(R.id.service_title);
                    //从网络加载数据填充应用服务图标
                    Glide.with(icon_view).load(Constant.Service_IP + service.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon_image);
                    icon_text.setText(service.getServiceName());//填充应用服务的名称
                    gridLayout.addView(icon_view);//将应用图标添加进图标区域中
                    rootline.addView(gridLayout);
                } else {
                    //获取应用服务图标与名字布局
                    icon_view = LayoutInflater.from(getContext()).inflate(R.layout.item_service_topic, (ViewGroup) getView(), false);
                    icon_image = icon_view.findViewById(R.id.service_icon);
                    icon_text = icon_view.findViewById(R.id.service_title);
                    //从网络加载数据填充应用服务图标
                    Glide.with(icon_view).load(Constant.Service_IP + service.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon_image);
                    icon_text.setText(service.getServiceName());//填充应用服务的名称
                    gridLayout.addView(icon_view);//将图标添加进图标区域中
                    rootline.addView(gridLayout);
                }
            } else if (!indexString.equals(service.getServiceType())) {//如果是新类型 就重新创建一个类型区域

                //将上一个服务区域布局添加到根布局上!!!!!!
                rootline.addView(gridLayout);

                indexString = service.getServiceType();//新的服务类型，将坐标字符串更新为新服务名称

                //首先创建一线条和类型名字
                line = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_more_line, null);
                TextView type_text = line.findViewById(R.id.service_type);
                type_text.setText(service.getServiceType());//设置类型名字
                rootline.addView(line);//在根布局上添加一行类型分割条//将布局添加到根布局上!!!!!!

                //再创建新的应用服务图标区域
                gridLayout = new GridLayout(getContext());
                gridLayout.setLayoutParams(gridParams);
                gridLayout.setColumnCount(5);//一排5个
                //获取应用服务图标与名字布局
                icon_view = LayoutInflater.from(getContext()).inflate(R.layout.item_service_topic, (ViewGroup) getView(), false);
                icon_image = icon_view.findViewById(R.id.service_icon);
                icon_text = icon_view.findViewById(R.id.service_title);
                //从网络加载数据填充应用服务图标
                Glide.with(icon_view).load(Constant.Service_IP + service.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon_image);
                icon_text.setText(service.getServiceName());//填充应用服务的名称
                gridLayout.addView(icon_view);//将图标添加进图标区域中
                //新的类型区域初始化完毕.....
            } else {
                //获取应用服务图标与名字布局
                icon_view = LayoutInflater.from(getContext()).inflate(R.layout.item_service_topic, (ViewGroup) getView(), false);
                icon_image = icon_view.findViewById(R.id.service_icon);
                icon_text = icon_view.findViewById(R.id.service_title);
                //从网络加载数据填充应用服务图标
                Glide.with(icon_view).load(Constant.Service_IP + service.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon_image);
                icon_text.setText(service.getServiceName());//填充应用服务的名称
                gridLayout.addView(icon_view);//将图标添加进图标区域中
            }
            /**
             * 在这里可以定义点击应用服务后的跳转操作
             */
            switch (list.get(i).getServiceName()) {
                case "停哪儿":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "停哪儿", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "城市地铁":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "城市地铁", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "智慧巴士":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "智慧巴士", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "门诊预约":
                    icon_view.setOnClickListener(v -> {
                        Navigation.findNavController(v).navigate(R.id.action_nav_more_to_hospitalFragment);
                    });
                    break;
                case "智慧交管":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "智慧交管", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "生活缴费":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "生活缴费", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "外卖订餐":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "外卖订餐", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "找房子":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "找房子", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "看电影":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "看电影", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "找工作":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "找工作", Toast.LENGTH_SHORT).show();
                    });
                    break;
                case "活动管理":
                    icon_view.setOnClickListener(v -> {
                        Navigation.findNavController(v).navigate(R.id.action_nav_more_to_activityHomeFragment);
                    });
                    break;
                case "数据分析":
                    icon_view.setOnClickListener(v -> {
                        Toast.makeText(getContext(), "数据分析", Toast.LENGTH_SHORT).show();
                    });
                    break;
            }
        }

    }

    /**
     * 按照服务类型进行排序
     */
    private class MySort implements Comparator<ServiceListBean.RowsBean> {

        @Override
        public int compare(ServiceListBean.RowsBean o1, ServiceListBean.RowsBean o2) {
            if (o1.getServiceType().compareToIgnoreCase(o2.getServiceType()) > 1) {
                return -1;
            }
            return 1;
        }
    }
}